package cn.tedu.weibo.service.impl;

import cn.tedu.weibo.exception.ServiceException;
import cn.tedu.weibo.mapper.WeiboMapper;
import cn.tedu.weibo.pojo.dto.WeiboDTO;
import cn.tedu.weibo.pojo.entity.Weibo;
import cn.tedu.weibo.pojo.vo.WeiboDetailVO;
import cn.tedu.weibo.pojo.vo.WeiboIndexVO;
import cn.tedu.weibo.response.StatusCode;
import cn.tedu.weibo.service.WeiboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class WeiboServiceImpl implements WeiboService {
    @Autowired
    private WeiboMapper weiboMapper;
    @Override
    public void addNew(WeiboDTO weiboDTO) {
        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboDTO,weibo);
        weibo.setCreated(new Date());
        int rows = weiboMapper.insert(weibo);
        if (rows==0){
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        }
    }

    @Override
    public List<WeiboIndexVO> listForIndex() {
        return weiboMapper.selectForIndex();
    }

    @Override
    public List<WeiboIndexVO> listForIndexByuserId(Long userId) {
        return weiboMapper.selectForIndexByUserId(userId);
    }

    @Override
    public void deleteById(Long id) {
        weiboMapper.deleteById(id);
    }

    @Override
    public WeiboDetailVO detailById(Long id) {
        return weiboMapper.detailById(id);
    }
}
